<?php

namespace Models;

use Phalcon\Mvc\Model\Validator\Email as Email;

class User extends \Models\ModelBase
{

    /**
     *
     * @var integer
     */
    public $id;
     
    /**
     *
     * @var string
     */
    public $name;
     
    /**
     *
     * @var string
     */
    public $email;
     
    /**
     * Validations and business logic
     */
    public function validation()
    {
        $this->validate(
            new Email(
                array(
                    "field"    => "email",
                    "required" => true,
                )
            )
        );
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }

    /**
     * @param string $class active record class name.
     * @return User the static model class
     */
    public static function model($class=__CLASS__)
    {
        return parent::model($class);
    }

    public function initialize()
    {
        $this->hasMany('id', 'Models\UserPost', 'idUser', array(
            'alias' => 'userPost'
        ));
    }

    public function getPost()
    {
        $userPost = $this->userPost;

        $result = array();

        if($userPost){
            foreach($userPost as $userP){
                $post = $userP->post;
                if($post && $post->isEnabled) $result[] = $post->toArray();
            }
        }

        return $result;
    }
}
